home *** CD-ROM | disk | FTP | other *** search
- /***************************************
-
- Main.c
- Driver Module for Application
-
- *****************************************/
-
- #include "headers.h"
- #include <limits.h>
- #include <Threads.h>
- #include "Protos.h"
- #include "menus.h"
- #include "CActiveXScheduler.h"
- #include "CSimpleAppSite.h"
- #include "App.h"
-
-
- /************************* Globals ***********************/
-
- static Boolean gControlHasFocus = false;
-
- Boolean gInBackGround = FALSE; /* Am I in the background? */
- Boolean gSendMouseUp = FALSE; /* Send the next mouse up event to the control? */
- Boolean gMouseInControl = false; // Is the mouse currently inside the control?
-
-
- #define OPTION_KEY(Event) ( (Event->modifiers & optionKey) != 0 )
-
- /******************* function prototypes *******************/
-
- static void AdjustCursor(EventRecord *theEvent);
-
- static void DoIdle(EventRecord* theEvent);
- static void DoKeyDown(EventRecord *theEvent);
- static void DoEvent(EventRecord *theEvent);
- static void DoActivateEvent(EventRecord *theEvent);
- static void DoHighLevelEvent(EventRecord *theEvent);
- static void DoMenu(long menuResult);
- static void DoMouseDown(EventRecord *theEvent);
- static void DoUpdate(WindowPtr window);
- static void DoCloseWindow(WindowPtr window, EventRecord *theEvent);
- static void DoContent(WindowPtr window, EventRecord *theEvent);
- static void DrawWindow(WindowPtr window);
-
- static void EventLoop(void);
-
- static long GetSleep(void);
-
-
-
-
- void main(void)
- {
- Initialize();
- EventLoop();
- }
-
- void EventLoop()
- /*
- Continuously get events and dispatch to the appropriate handler
- */
- {
- RgnHandle cursorRgn;
- Boolean ignoreResult;
- EventRecord theEvent;
-
- cursorRgn = NewRgn();
- while (TRUE)
- {
- ignoreResult = WaitNextEvent(everyEvent,&theEvent, 0,cursorRgn);
-
- AdjustCursor(&theEvent);
- DoEvent(&theEvent);
- }
- }
-
- void DoEvent(EventRecord *theEvent)
- /*
- Dispatch the event to the appropriate handler
- */
- {
- switch (theEvent->what)
- {
- case nullEvent:
- DoIdle(theEvent);
- break;
-
- case mouseDown:
- DoMouseDown(theEvent);
- break;
-
- case mouseUp:
- if ( gControl && gSendMouseUp )
- {
- gControl->DoMouse(MouseUp, theEvent);
- gSendMouseUp = FALSE;
- }
- break;
-
- case keyDown:
- case autoKey:
- DoKeyDown(theEvent);
- break;
-
- case activateEvt:
- DoActivateEvent(theEvent);
- break;
-
- case updateEvt:
- DoUpdate((WindowPtr) theEvent->message);
- break;
-
- case kHighLevelEvent:
- DoHighLevelEvent(theEvent);
- break;
-
- }
-
- CActiveXScheduler* Scheduler = CActiveXScheduler::GetActiveXScheduler();
- if (Scheduler)
- Scheduler->Idle(theEvent->what == nullEvent);
- }
-
- void DoIdle(EventRecord* theEvent)
- {
- Point testPt;
-
- YieldToAnyThread();
-
- if ( gControl )
- {
- // if the mouse is in a control rectangle,
- testPt = theEvent->where;
- GlobalToLocal(&testPt);
-
- // if the click is in the control, send it the mouse down event
- if ( PtInRect(testPt, &gControlRect) )
- {
- if ( !gMouseInControl )
- {
- gControl->DoMouse(MouseEnter, theEvent);
- gMouseInControl = true;
- }
- }
- else if ( gMouseInControl )
- {
- gControl->DoMouse(MouseLeave, theEvent);
- gMouseInControl = false;
- InitCursor();
- }
- }
- }
-
- void DoActivateEvent(EventRecord *theEvent)
- {
- DrawContext Context = {(PortType)0};
- WindowPtr window;
-
- window = (WindowPtr) theEvent->message;
- if ((theEvent->modifiers & activeFlag) != 0)
- {
- DoActivate(window);
-
- if ( gControl )
- {
- gControl->DoActivate(WindowActivate, 1, theEvent);
- }
- }
- else
- {
- if ( gControl )
- {
- gControl->DoActivate(WindowDeactivate, 1, theEvent);
- }
-
- DoDeactivate(window);
- }
- }
-
- void DoHighLevelEvent(EventRecord *theEvent)
- {
- if ( gControl )
- gControl->DoSystemEvent(theEvent);
- }
-
-
- void AdjustCursor(EventRecord *theEvent)
- {
- Point testPt;
-
- testPt = theEvent->where;
- GlobalToLocal(&testPt);
- }
-
- void DoUpdate(WindowPtr window)
- /*
- handle an update event detected by GetNextEvent
- */
- {
- BeginUpdate(window);
- DrawWindow(window);
- EndUpdate(window);
- }
-
- void DoMouseDown(EventRecord *theEvent)
- /*
- handle a mouseDown event
- */
- {
- short part;
- WindowPtr window = NULL;
-
- part = FindWindow(theEvent->where, &window);
-
- switch (part)
- {
- case inMenuBar:
- AdjustMenus();
- DoMenu(MenuSelect(theEvent->where) );
- break;
-
- case inSysWindow:
- SystemClick(theEvent,window);
- break;
-
- case inContent:
- DoContent(window,theEvent);
- break;
-
- case inDrag:
- DragWindow(window,theEvent->where,&qd.screenBits.bounds);
- SetPort(window);
- break;
-
- case inGoAway:
- if (TrackGoAway(window,theEvent->where))
- DoCloseWindow(window, theEvent);
- break;
-
- case inGrow:
- //GrowWindow(theEvent->where);
- break;
-
- case inZoomIn:
- case inZoomOut:
- ZoomWindow(window, part, TRUE);
- break;
- }
- }
-
- long GetSleep()
- {
- long sleep;
-
- sleep = LONG_MAX;
- return sleep;
- }
-
- void DoMenu(long menuResult)
- /*
- Handle a user menu selection
- */
- {
- short menuID;
- short menuItem;
-
- menuID = HiWord(menuResult);
- menuItem = LoWord(menuResult);
-
- switch (menuID)
- {
- case mApple:
- DoAppleMenu(menuID,menuItem);
- break;
-
- case mFile:
- DoFileMenu(menuItem);
- break;
-
- case mEdit:
- DoEditMenu(menuItem);
- break;
-
- case mColor:
- DoColorMenu(menuItem);
- break;
-
- }
- HiliteMenu(0);
- }
-
- void DrawWindow(WindowPtr window)
- /*
- draw the window for which an update event was detected
- */
- {
- GrafPtr SavePort;
- DrawContext Context = {(PortType)0};
-
- GetPort(&SavePort);
- SetPort(window);
-
- ::EraseRect(&window->portRect);
-
- if ( gControl )
- {
- // Draw a border around the control
- Point InPenSize = window->pnSize;
- Rect BorderRect = gControlRect;
-
- ::InsetRect(&BorderRect, -8, -8);
- ::PenSize(7, 7);
- ::FrameRect(&BorderRect);
- ::PenSize(InPenSize.h, InPenSize.v);
-
- // Draw the control
- if (gSite->AcquireContext(WindowContextID, &Context) == S_OK)
- {
- gControl->Draw(&Context);
- gSite->ReleaseContext(&Context);
- }
- #ifdef USE_OFFSCREEN
- if (gSite->AcquireContext(OffscreenContextID, &Context) == S_OK)
- {
- gControl->Draw(&Context);
- gSite->ReleaseContext(&Context);
- {
- Rect DestRect = { 0, 300, 290, 496};
-
- // blit to the screen
- ::ForeColor(blackColor);
- ::BackColor(whiteColor);
- CopyBits((BitMap*)(*(gAppData.Offscreen->portPixMap)),
- &((GrafPtr)gAppData.Window)->portBits, &gControlRect, &DestRect, srcCopy, NULL);
- }
- }
- #endif // USE_OFFSCREEN
- }
- else
- {
- short OldTextSize = window->txSize;
-
- TextSize(24);
- MoveTo(50, 50);
- DrawString("\pDrop a control into the window.");
- TextSize(OldTextSize);
- }
-
- SetPort(SavePort);
- }
-
- void DoContent(WindowPtr window, EventRecord *theEvent)
- /*
- Handle a user mouse click in a particular window
- */
- {
- Point testPt;
-
- if ( IsAppWindow(window) && gControl )
- {
- SetPort(window);
- testPt = theEvent->where;
- GlobalToLocal(&testPt);
-
- // if the click is in the control, send it the mouse down event
- if ( PtInRect(testPt, &gControlRect) )
- {
- gControl->SetFocus(TakeNextCommand, FullFocusSet);
- gControl->DoMouse(MouseDown, theEvent);
- gSendMouseUp = true;
- gControlHasFocus = true;
- }
- else
- {
- if (gControlHasFocus)
- {
- gControl->SetFocus(ReleaseCommand, FullFocusSet);
- gControlHasFocus = false;
- }
- }
- }
-
- }
-
- void DoKeyDown(EventRecord *theEvent)
- /*
- respond to user keypresses
- */
- {
- char key;
- WindowPtr frontMost;
- long menuResult;
- KeyEventType theKeyET;
-
- frontMost = FrontWindow();
- if (!IsDAWindow(frontMost))
- {
- key = theEvent->message & charCodeMask;
- menuResult = 0;
-
- if ((theEvent->modifiers & cmdKey) != 0)
- {
- unsigned char menukey = key;
-
- AdjustMenus();
-
- menuResult = MenuKey(menukey);
- if (menuResult != 0)
- DoMenu(menuResult);
- }
-
- if ( menuResult == 0 && gControlHasFocus )
- {
- if (theEvent->what == autoKey)
- theKeyET = AutoKey;
- else
- theKeyET = KeyDown;
-
- if ( gControl )
- gControl->DoKey(theKeyET, key, theEvent);
- }
-
- }
- }
-
- void DoCloseWindow(WindowPtr window, EventRecord *theEvent)
- {
- #pragma unused (theEvent)
- if ( gControl )
- RemoveControl();
- DisposeWindow(window);
- }
-
-
-